import threading

num = 0

def add1():

    # 下边已经创建了锁  直接使用acquire  加锁  这样其他的就只能等待
    mutex.acquire()
    for i in range(10000000):
        global num
        num += 1
    print(num)
    print('\n')
    # 释放锁
    mutex.release()


def add2():
    mutex.acquire()
    for i in range(10000000):
        global num
        num += 1
    print(num)
    mutex.release()

if __name__ == '__main__':
    # 创建锁
    mutex = threading.Lock()
    a = threading.Thread(target=add1)
    b = threading.Thread(target=add2)
    a.start()
    b.start()
